# Contributor: Marian Buschsieweke <marian.buschsieweke@posteo.net>
# Maintainer: Marian Buschsieweke <marian.buschsieweke@posteo.net>
pkgname=py3-litex
_pkgname=litex
pkgver=2024.04
pkgrel=0
pkgdesc="infrastructure to create FPGA Cores/SoCs and full FPGA based systems"
url="https://github.com/enjoy-digital/litex"
license="BSD-2-Clause"
# ppc64le, aarch64: blocked by verilator
# s390x, armv7, armhf, riscv64, loongarch64: blocked by verilator, picolibc-riscv-none-elf
# x86: unit test failures
arch="all !ppc64le !s390x !armv7 !armhf !riscv64 !aarch64 !x86 !loongarch64"
depends="
	py3-migen
	py3-packaging
	py3-pyserial
	py3-requests
	py3-yaml
	python3
	"
makedepends="
	py3-setuptools
	py3-gpep517
	py3-wheel
	py3-installer
	"
checkdepends="
	bash
	json-c-dev
	libevent-dev
	linux-headers
	meson
	picolibc-riscv-none-elf
	py3-litex-hub-modules
	py3-pexpect
	py3-pytest
	verilator
	verilator-dev
	zlib-dev
	"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/enjoy-digital/litex/archive/refs/tags/$pkgver.tar.gz
	litescope-$pkgver.tar.gz::https://github.com/enjoy-digital/litescope/archive/refs/tags/$pkgver.tar.gz
	litex-boards-$pkgver.tar.gz::https://github.com/litex-hub/litex-boards/archive/refs/tags/$pkgver.tar.gz

	0002-litex-fix-invalid-escape-sequence.patch
	0003-litepcie-fix-package-absent-from-packages-configuration.patch
	"
builddir="$srcdir"
subpackages="
	$pkgname-pyc
	$pkgname-full
	py3-litescope
	py3-litex-boards
	"

_cores_enjoy_digital="
	dram:DRAM
	eth:Ethernet
	iclink:Inter-Chip-Communication
	jesd204b:JESD204B
	pcie:PCIe
	sata:SATA
	sdcard:SDCard
	"
_cores_litex_hub="
	spi:SPI
	"
_cores="$_cores_enjoy_digital $_cores_litex_hub"

for _core_name in $_cores_enjoy_digital; do
	_core=${_core_name%:*}
	subpackages="$subpackages py3-lite$_core:_subpkg_core"
	source="$source py3-lite$_core-$pkgver.tar.gz::https://github.com/enjoy-digital/lite$_core/archive/refs/tags/$pkgver.tar.gz"
done

for _core_name in $_cores_litex_hub; do
	_core=${_core_name%:*}
	subpackages="$subpackages py3-lite$_core:_subpkg_core"
	source="$source py3-lite$_core-$pkgver.tar.gz::https://github.com/litex-hub/lite$_core/archive/refs/tags/$pkgver.tar.gz"
done

prepare() {
	default_prepare

	# remove and rebuild x86_64 glibc binaries used in litesata test
	cd "$srcdir/litesata-$pkgver/test/model"
	rm crc scrambler
	gcc -Os -o scrambler scrambler.c
	gcc -Os -o crc crc.c
}

build() {
	echo "--> Building LiteX"
	cd "$srcdir/litex-$pkgver"
	gpep517 build-wheel \
		--wheel-dir dist \
		--output-fd 3 3>&1 >&2

	echo "--> Building LiteX-Boards"
	cd "$srcdir/litex-boards-$pkgver"
	gpep517 build-wheel \
		--wheel-dir dist \
		--output-fd 3 3>&1 >&2

	echo "--> Building LiteScope"
	cd "$srcdir/litescope-$pkgver"
	gpep517 build-wheel \
		--wheel-dir dist \
		--output-fd 3 3>&1 >&2

	for _core_name in $_cores; do
		_core=$(echo "$_core_name" | cut -d : -f 1)
		_name=$(echo "$_core_name" | cut -d : -f 2)
		echo "--> Building LiteX core $_name"

		cd "$srcdir/lite$_core-$pkgver"
		gpep517 build-wheel \
			--wheel-dir dist \
			--output-fd 3 3>&1 >&2
	done
}

check() {
	cd "$srcdir/litex-$pkgver"
	python3 -m venv --clear --without-pip --system-site-packages "$srcdir"/testenv
	"$srcdir"/testenv/bin/python3 -m installer dist/*.whl
	"$srcdir"/testenv/bin/python3 -m installer "$srcdir/litex-boards-$pkgver"/dist/*.whl
	"$srcdir"/testenv/bin/python3 -m installer "$srcdir/litescope-$pkgver"/dist/*.whl

	for _core_name in $_cores; do
		_core=$(echo "$_core_name" | cut -d : -f 1)
		"$srcdir"/testenv/bin/python3 -m installer "$srcdir/lite$_core-$pkgver"/dist/*.whl
	done

	echo "--> Testing LiteX"
	# CPU test fails with "%Error: Verilator internal fault, sorry. Suggest trying --debug --gdbbt"
	env PATH="$srcdir/testenv/bin:$PATH" "$srcdir"/testenv/bin/python3 -m pytest -v \
		--deselect test/test_cpu.py::TestCPU::test_cpu

	for _core_name in $_cores; do
		_core=$(echo "$_core_name" | cut -d : -f 1)
		_name=$(echo "$_core_name" | cut -d : -f 2)
		echo "--> Testing LiteX core $_name"
		cd "$srcdir/lite$_core-$pkgver"

		# running tests now, but deselecting two of the slowest tests
		# (when unlucky, the run for longer than the 12 minute timeout and
		# fail on the builder).
		env PATH="$srcdir/testenv/bin:$PATH" "$srcdir/testenv/bin/python3" -m pytest -v \
			--deselect test/test_lpddr4.py::VerilatorLPDDR4Tests::test_lpddr4_sim_fast \
			--deselect test/test_lpddr4.py::VerilatorLPDDR4Tests::test_lpddr4_sim_x2rate_no_cache \
			#

	done
}

package() {
	python3 -m installer -d "$pkgdir" \
		"$srcdir/litex-$pkgver"/dist/*.whl

	for _core_name in $_cores; do
		_core=$(echo "$_core_name" | cut -d : -f 1)
		python3 -m installer -d "$pkgdir" \
			"$srcdir/lite$_core-$pkgver"/dist/*.whl
	done

	python3 -m installer -d "$pkgdir" \
		"$srcdir/litescope-$pkgver"/dist/*.whl

	python3 -m installer -d "$pkgdir" \
		"$srcdir/litex-boards-$pkgver"/dist/*.whl
}

_subpkg_core() {
	_core="${subpkgname#py3-lite}"
	for _core_name in $_cores; do
		if [ "$_core" = "$(echo "$_core_name" | cut -d : -f 1)" ]; then
			_name=$(echo "$_core_name" | cut -d : -f 2)
		fi
	done
	pkgdesc="Small footprint and configurable $_name core"
	depends="$depends py3-litex"

	amove usr/lib/python3*/site-packages/"lite$_core"
	amove usr/lib/python3*/site-packages/"lite$_core"-*.dist-info
}

litescope() {
	pkgdesc="Small footprint and configurable embedded FPGA logic analyzer"
	depends="$depends py3-litex"
	amove usr/lib/python3*/site-packages/litescope
	amove usr/lib/python3*/site-packages/litescope-*.dist-info
}

boards() {
	pkgdesc="LiteX boards files"
	depends="$depends py3-litex"
	amove usr/lib/python3*/site-packages/litex_boards
	amove usr/lib/python3*/site-packages/litex_boards-*.dist-info
}

full() {
	pkgdesc="Meta package to install full LiteX framework"
	depends="
		bash
		json-c-dev
		libevent-dev
		linux-headers
		meson
		picolibc-riscv-none-elf
		py3-litescope
		py3-litex
		py3-litex-boards
		py3-litex-hub-modules
		verilator
		verilator-dev
		zlib-dev
		"
	for _core_name in $_cores; do
		_core=$(echo "$_core_name" | cut -d : -f 1)
		depends="$depends py3-lite$_core"
	done

	mkdir -p "$subpkgdir"
}

sha512sums="
d99c90864c07a31a978737bce44ab9efca2c6bc373dfac8b4b9d09a717e8795522583b9ad0f8425e8226d9ae675552a39e72be5ae44a1d070bcdccd13f2ad767  py3-litex-2024.04.tar.gz
686df31b659a64bf10bcfa17afd8618606f24970edd0037a6ba116f59f89f50f985aa8cf3c2363ce509565509eaa5197444ba506aaec88564364ddda23242bc4  litescope-2024.04.tar.gz
ffb9cda9cceda3d0f89dd830f36311a213141aef577eb0f84214cf4d0f2edbecc4ddb6f33a9ead50f8e38461a7d07add9900825fbe7617ee4d61daac9b14cfad  litex-boards-2024.04.tar.gz
0a21c64f6045efdc2ecef3c956f9977ee28bb96f96e6f76567e223c394fe818249f2bd358b5a23fa8072c4932a7b428d74624a7be4ab179411296cbf86cf05c3  0002-litex-fix-invalid-escape-sequence.patch
fb70624eb3c46bb781612b6d86501b11956c4704406124bb050f4dc2c72bda0787be7c37bd2879769aa4d480b2fbd5aa8504121341ee7e01690d3b816b940492  0003-litepcie-fix-package-absent-from-packages-configuration.patch
277625436718a00299f5896e38be0a50dbcb25ff0a398d67f3e27057c2fa8ffe88b60d5e5188e35d9e51a032db04dbf83dd4a592000fe9f195b17c2d4f293293  py3-litedram-2024.04.tar.gz
9887be60520643c23f8822dc56adea1e3e553ea1fe62b88023f3b2da6b7f3a9835ed6f84283e6b899d8fab78421f64b8f0dc6347e30c02e5f4f910ccc73024eb  py3-liteeth-2024.04.tar.gz
56d7de1b14a534c2c1325166bba55e85f79144bbdd91e0c4e87eb620f9fb099e001bc545438e3ad757f619d6c68a5dd9807d6a597e18b541e9347d7e9a505484  py3-liteiclink-2024.04.tar.gz
353d9e89a7744dd30012082694d9a5bb4926e4917565320eec0f62adf910e3f5bc83afa8b6e197dce507177e78605eccca6abaa5dc52567224f6a41cb01b795d  py3-litejesd204b-2024.04.tar.gz
c73fc952739111e8fdecc9ea0e2b11245fc8f5fab4f8fb8fc45da470b1a5b06a5158ce77c1db5f67bba240a8c18bca9e0594c904959b9f9d832e13d7dd147197  py3-litepcie-2024.04.tar.gz
adccfa677e513acf846963c015ba4a16bc2c190f094b1c66030943a9bd6cecd93d5b8a23bb73602e5302d317554ebcfae20da4035de6a4736f27d66ff3016a8b  py3-litesata-2024.04.tar.gz
3cfefc4253529eae61f371573f6073460aebd785fbbe984714862418a808d85aff953a592d8b18758c399c07100d1374541da30051cdbf5d0aef7f58df20216a  py3-litesdcard-2024.04.tar.gz
5afe80eebfe35e3850d4b2edf46f3d79e9bd48282b1c293f3cb1925707776d42550857d2b8c0e02dc10a9118fea8ebe87f87d2d0bafb47e1cf25fc1cedf966a2  py3-litespi-2024.04.tar.gz
"
